/*
 * SpaceClient.cpp
 *
 *  Created on: 1 mar 2010
 *      Author: Andreas
 */

#include "SpaceClient.h"
#include <sstream>
#include "SpaceApp.h"
#include "engine/util/string_manip.h"

SpaceClient::SpaceClient(string ip, int port, SpaceApp* parent)
 : Client(ip, port) {
	this->parent = parent;
	lastKeys = "";
}

void SpaceClient::update(int elapsed) {
	Player *p = parent->getPlayers()[parent->getPlayerIndex()];

	/***** SEND *****/
	posTimer += elapsed;

	/* Player position */
	if (posTimer >= 1000) {
		stringstream ss;
		ss << "10;" << (int)p->getXpos() << ";" << (int)p->getYpos() << ";" << p->getAngle();
		send(ss.str());
		posTimer -= 1000;
	}

	/* Player keys */
	{
		stringstream ss;
		ss << "11";
		for (int i=0; i<4; i++) {
			ss << ";" << p->getKeyDown(i);
		}
		if (lastKeys != ss.str()) {
			lastKeys = ss.str();
			send(lastKeys);
		}
	}
	/* bullet */
	while(p->getSendBullet()>0){
			cout<<"megakill"<<endl;
			stringstream ss;
			float angle =p->getBullets().back()->getAngle();
			int x =(int)p->getBullets().back()->getXpos();
			int y =(int)p->getBullets().back()->getYpos();
			float dx =p->getBullets().back()->getDx();
			float dy =p->getBullets().back()->getDy();
			int type =p->getBullets().back()->getType();
			ss << "12;" << x << ";" << y << ";" << angle << ";" << dx << ";" << dy << ";" << type;
			send(ss.str());
			p->setSendBullet(p->getSendBullet()-1);
	}



	/***** RECEIVE *****/
	while(true){
		string msg = receive();

		vector<Player *> players = parent->getPlayers();
		vector<string> results;
		results = split(msg, ";"); /* Split the message by ';' */

		if( msg == ""){
			break;
		}
		else if(results[0] == "1") {
			if (!accepted) {
				accepted = true;
				parent->emptyPlayers();
				for (int i=0; i < atoi(results[1].c_str())-1; i++){
					cout<<"adding player:"<<results[1]<<endl;
					Player *p = new Player(Player::SHIP_BLUE);
					parent->addPlayer(p);
				}
				int keys[]={SDLK_w,SDLK_a,SDLK_s,SDLK_d,SDLK_f};
				Player *p = new Player(keys, Player::SHIP_RED);
				parent->addPlayer(p);
				parent->setPlayerIndex(toi(results[1])-1);
				cout << "connection etablished" << endl;
			}
			else {
				Player *p = new Player(Player::SHIP_BLUE);
				parent->addPlayer(p);
			}
		}// Player pos
		else if (results[0] == "10" ) {
			int x = atoi(results[1].c_str());
			int y = atoi(results[2].c_str());
			float angle = atof(results[3].c_str());
			int user = toi(results[4]);

			Player *p = players[user];
			p->moveTo(x, y);
			p->setAngle(angle);
		}
		// Player keys
		else if (results[0] == "11") {
			int keys[5];
			for (int i=0; i<4; i++) {
				keys[i] = atoi(results[i+1].c_str());
			}
			keys[4] = 0;
			int user = toi(results[5]);
			players[user]->setKeysDown(keys);
		}else if(results[0] == "12"){
			cout << msg << endl;
			int x = atoi(results[1].c_str());
			int y = atoi(results[2].c_str());
			float angle = atof(results[3].c_str());
			float dx = atof(results[4].c_str());
			float dy = atof(results[5].c_str());
			int type = atoi(results[6].c_str());
			int user = toi(results[7]);
			Player *p = players[user];
			p->addBullet(x,y,angle,dx,dy,type);
		}
	}
}
